<?php

// 初期設定
if (!defined("IN_APP")) {
	define("IN_APP", true);
	include_once("../../include/init.php");
	requireLogin();
}

// ログインしていないユーザーを転送する
if (!LOGGED_IN) {
	$_SESSION["message"] = $MESSAGES["ACCESS_FORBIDDEN"];
	header("Location: {$SETTINGS['SCRIPT_PATH']}");
}

//　タブ別の設定の読み込み
require_once("tab.include.php");

// ページ名
$page_name = "view_group_menu";

/******************* ページの処理　*******************/

class GroupMember {
	var $name, $twitter_username, $full_name;
}

// 表示されている名前をフォーマットする
function formatName($name, $twitter_username) {
	if (empty($name))
		return $twitter_username;
	else
		return $name . " (" . $twitter_username . ")";	
}

$group_members = array();
$leader_name = "";

// グループidをセッションから読み込む
$group_id = $_SESSION['group'];

$connect =  connectToDb();


// グループ情報を取得
$query = sprintf("SELECT title,owner_id FROM groups 
				  WHERE group_id = %s AND (del_flag <> 'd' OR del_flag IS NULL)",
							mysql_real_escape_string($group_id));

$result = mysql_query($query, $connect);


if ($result) {
	$row = mysql_fetch_assoc($result);
	
	$group_name = $row['title'];
	$leader_id = $row['owner_id'];
	
	// グループメンバー情報を取得
	$query = "SELECT users.user_id,twitter_username FROM group_members ";
	$query.= "INNER JOIN users ON group_members.user_id = users.user_id ";
	$query.= "WHERE group_id = " . mysql_real_escape_string($group_id) . " ";
	$query.= "AND (group_members.del_flag <> 'd' OR group_members.del_flag IS NULL)";
	$query.= "ORDER BY group_members.sort_order ASC";
	
	$result = mysql_query($query, $connect);
	
	while ($row = mysql_fetch_assoc($result)) {
	
		$username = getUserName($connect, $group_id, $row['user_id']);
	
		if ($row['user_id'] == $leader_id) {
			$leader_name = formatName($username, $row['twitter_username']);
		}
		
		$member = new GroupMember();
		$member->name = $username;
		$member->twitter_username = $row['twitter_username'];
		$member->full_name = formatName($username, $row['twitter_username']);
		array_push($group_members, $member);
	}
	
	resetHistoryMemory();
}

//　リーダーかどうかを確認する
if ($_SESSION['user_id'] == $leader_id)
	$leader = true;
else
	$leader = false;

mysql_close($connect);

/******************* 処理終了 *******************/

//　ページを表示する
$include_path = $SETTINGS['VIEW_PATH'] . $tab_name . "/" . $page_name . $SETTINGS['VIEW_NAME'];
include($include_path);

?>